package edu.usc.cmb.iregulome.web;

import java.io.FilePermission;
import java.io.IOException;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.log4j.PropertyConfigurator;

public class InitServletContextListener implements ServletContextListener {

	@Override
	public void contextDestroyed(ServletContextEvent event) {
	}

	/**
	 * initial the 
	 */
	@Override
	public void contextInitialized(ServletContextEvent event) {
		ServletContext context = event.getServletContext();

		// store servlet abs path to system environment variable rootPath

		System.out.println(context.getRealPath("/"));
		System.setProperty("rootPath", context.getRealPath("/"));
		PropertyConfigurator.configure(context
				.getRealPath("/WEB-INF/classes/applog.properties"));
		String scriptPath = context.getRealPath("/WEB-INF/script/");
		try {
			// make wigToBigWig executable
			Runtime.getRuntime().exec("chmod +x "+scriptPath + "/wigToBigWig");
			
			new FilePermission(scriptPath + "/wigToBigWig", "execute");
			// make bedToBigBed executable
			new FilePermission(scriptPath + "/bedToBigBed", "execute");
			Runtime.getRuntime().exec("chmod +x "+scriptPath + "/bedToBigBed");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
